package com.qf.controller;

import com.qf.pojo.Goods;
import com.qf.pojo.PageBean;
import com.qf.service.GoodsService;
import com.qf.service.impl.GoodsServiceImpl;
import com.qf.util.StrUtils;
import com.qf.vo.GoodsVo;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 商品管理controller
 * @author 千锋健哥
 */
@WebServlet("/goods")
public class GoodsController extends BaseController{

    /**
     * 根据商品id查询商品详情, 跳转到详情页面显示
     * @param request
     * @param response
     * @return
     */
    public String detail(HttpServletRequest request, HttpServletResponse response) {
        //1. 获取商品id
        String id = request.getParameter("id");

        //2. 根据商品id调用service查询
        GoodsService goodsService = new GoodsServiceImpl();
        Goods goods = goodsService.detail(Integer.valueOf(id));

        //3. 将数据放入request域中
        request.setAttribute("goods", goods);

        //4. 页面跳转
        return "forward:/goodsDetail.jsp";

    }

    /**
     * 商品分页查询
     * @param request
     * @param response
     * @return
     */
    public String goodsPage(HttpServletRequest request, HttpServletResponse response) {
        //1. 获取参数
        //查询的商品名称
        String goodsName = request.getParameter("goodsName");
        //当前页
        String currentPageStr = request.getParameter("currentPage");
        Integer currentPage = 1;

        if (StrUtils.empty(currentPageStr)) {
            //当传入的当前页字符串不为空的时候, 进行转换当前页为integer类型
            currentPage = Integer.parseInt(currentPageStr);
        }
        if (currentPage < 1) {
            currentPage = 1;
        }

        //2.封装分页对象
        PageBean pageBean = new PageBean(null, currentPage, 8, 0);

        //3. 调用service查询
        GoodsService goodsService = new GoodsServiceImpl();
        pageBean = goodsService.findPage(goodsName, pageBean);

        //4. 将返回结果放入request域中
        request.setAttribute("pageBean", pageBean);
        request.setAttribute("goodsName", goodsName);

        //5. 页面跳转
        return "forward:/goodsList.jsp";
    }

    /**
     * 查询所有商品列表
     */
    public String goodsList(HttpServletRequest request, HttpServletResponse response) {
        //1. 获取请求参数
        //获取商品名称参数
        String name = request.getParameter("name");
        //获取上架时间参数
        String pubdate = request.getParameter("pubdate");

        //2. 调用service进行查询
        GoodsService goodsService = new GoodsServiceImpl();
        List<GoodsVo> list = goodsService.findList(name, pubdate);

        //3. 将结果放入请求域中
        request.setAttribute("goodsList", list);
        //查询条件回显
        request.setAttribute("name", name);
        request.setAttribute("pubdate", pubdate);

        //4. 请求转发到商品列表页面
        return "forward:/admin/showGoods.jsp";
    }

    /**
     * 添加商品保存
     * @param request
     * @param response
     * @return
     */
    public String addGoods(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //1. 获取数据
        String name = request.getParameter("name");
        String typeid = request.getParameter("typeid");
        String pubdate = request.getParameter("pubdate");
        String price = request.getParameter("price");
        String star = request.getParameter("star");
        String intro = request.getParameter("intro");
        String picture = request.getParameter("picture");

        //2. 校验数据
        if (!StrUtils.empty(name)) {
            return "forward:/admin/addGoods.jsp";
        }
        if (!StrUtils.empty(typeid)) {
            return "forward:/admin/addGoods.jsp";
        }
        if (!StrUtils.empty(pubdate)) {
            return "forward:/admin/addGoods.jsp";
        }
        if (!StrUtils.empty(price)) {
            return "forward:/admin/addGoods.jsp";
        }
        if (!StrUtils.empty(star)) {
            return "forward:/admin/addGoods.jsp";
        }
        if (!StrUtils.empty(picture)){
            return "forward:/admin/addGoods.jsp";
        }

        //3. 将数据封装成商品实体类
        Goods goods = new Goods();
        goods.setName(name);
        goods.setIntro(intro);
        goods.setPicture(picture);
        goods.setPrice(Integer.parseInt(price));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        goods.setPubdate(sdf.parse(pubdate));
        goods.setStar(Integer.parseInt(star));
        goods.setTid(Integer.parseInt(typeid));

        //4. 调用service完成保存
        GoodsService goodsService = new GoodsServiceImpl();
        goodsService.add(goods);

        //5. 保存后跳转到商品列表查询controller方法
        return "redirect:/goods?method=goodsList";

    }

}
